package com.uwo.house.controller;

import com.uwo.house.beans.request.InfoAboutReq;
import com.uwo.house.beans.request.SupplierPartnerReq;
import com.uwo.house.beans.response.*;
import com.uwo.house.config.SmsContentConfig;
import com.uwo.house.kits.SmsKits;
import com.uwo.house.service.IAdministratorService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by cyn1c on 2017/8/19.
 */
@Controller
@RequestMapping("/admin")
public class AdministratorController{
    @Autowired
    private IAdministratorService administratorService;

    /**
     * 关于我们编辑
     * @param infoAboutReq
     * @return
     */
    @RequestMapping(value = "/aboutus", method = RequestMethod.POST)
    @ResponseBody
    public Map addOrUpdateInfo(@RequestBody InfoAboutReq infoAboutReq) {
        int result = this.administratorService.addInfo(infoAboutReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     *获取关于我们
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getCompanyAbout() {
        InfoAboutResp infoAboutResp = this.administratorService.getCompanyAbout();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, infoAboutResp);
    }

    /**
     * 服务流程图片添加
     * @param flowPic
     * @return
     */
    @RequestMapping(value = "/serviceflow", method = RequestMethod.POST)
    @ResponseBody
    public Map addServiceFlow(@RequestParam("flowPic") String flowPic) {
        int result = this.administratorService.addServiceFlow(flowPic);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 获取服务流程图片
     *
     * @return
     */
    @RequestMapping(value = "/serviceflow/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getServiceFlow() {
        String flowPic = this.administratorService.getServiceFlow();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, flowPic);
    }


    /**
     * 添加快递公司
     * @param deliverCompany
     * @return
     */
    @RequestMapping(value = "/delivercompany/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addDeliverCompany(@RequestParam("deliverCompany") String deliverCompany) {
        int result = this.administratorService.addDeliverCompany(deliverCompany);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 删除快递公司
     * @param id
     * @return
     */
    @RequestMapping(value = "/delivercompany/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteDeliverCompany(@RequestParam("id") int id) {
        int result = this.administratorService.deleteDeliverCompany(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 查询快递公司列表
     * @param deliverCompany
     * @return
     */
    @RequestMapping(value = "/delivercompany/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getDeliverCompany(@RequestParam(value = "deliverCompany",required = false)String deliverCompany){
        List<DeliverCompanyResp> list = this.administratorService.getDeliverCompany(deliverCompany);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 通用-设定金额
     *
     * @param deposit
     * @param reward
     * @return
     */
    @RequestMapping(value = "/moneyset", method = RequestMethod.POST)
    @ResponseBody
    public Map priceSetAction(@RequestParam("deposit") BigDecimal deposit,
                              @RequestParam("reward") BigDecimal reward) {
        int result = this.administratorService.addMoneySet(deposit, reward);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 添加合作供应商
     * @param supplierPartnerReq
     * @return
     */
    @RequestMapping(value = "/supplier/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addSupplierPartner(@RequestBody SupplierPartnerReq supplierPartnerReq){
        int result = this.administratorService.addSupplierPartner(supplierPartnerReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 删除合作供应商
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/supplier/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteSupplierPartner(@RequestParam("id") int id) {
        int result = this.administratorService.deleteSupplierPartner(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 获取合作供应商列表
     *
     * @return
     */
    @RequestMapping(value = "/supplier/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getSupplierPartnerList() {
        List<SupplierPartnerResp> list = this.administratorService.getSupplierPartnerList();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 编辑合作供应商
     * @param supplierPartnerReq
     * @return
     */
    @RequestMapping(value = "/supplier/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map modifySupplierPartner(@RequestBody SupplierPartnerReq supplierPartnerReq){
        int result = this.administratorService.modifySupplierPartner(supplierPartnerReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "操作成功");
        }
        return UtilReturn.resErrorMap("操作失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 获取奖励金额
     *
     * @return
     */
    @RequestMapping(value = "/reward/get", method = RequestMethod.GET)
    @ResponseBody
    public Map getRewardMoney() {
        int rewardMoney = this.administratorService.getRewardMoney();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, rewardMoney);
    }

    /**
     * 获取订金金额
     *
     * @return
     */
    @RequestMapping(value = "/deposit/get", method = RequestMethod.GET)
    @ResponseBody
    public Map getDepositMoney() {
        BigDecimal rewardMoney = this.administratorService.getDepositMoney();
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, rewardMoney);
    }


    /**
     * 获取材料订单列表
     *
     * @return
     */
    @RequestMapping(value = "/material/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialOrderList(@RequestParam(value = "orderNo", required = false) String orderNo,
                                    @RequestParam(value = "supplierNo", required = false) String supplierNo,
                                    @RequestParam(value = "preOrderStatus",required = false) String preOrderStatus,
                                    @RequestParam(value = "deliverGoodsStatus", required = false) String deliverGoodsStatus) {
        List<MaterialOrderResp> list = this.administratorService.getMaterialOrderList(orderNo, supplierNo, preOrderStatus, deliverGoodsStatus);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 材料订单-下单
     *
     * @return
     */
    @RequestMapping(value = "/supplier/place", method = RequestMethod.POST)
    @ResponseBody
    public Map placeOrder(@RequestParam("id") Integer id) {
        int result = this.administratorService.placeOrder(id);
        if (result >= 1) {
            String mobile = administratorService.getPhoneByMaterialId(id);
            try {
                SmsKits.send(SmsContentConfig.materialOrderRemind, mobile);
            } catch (Exception e) {
                System.out.print("发送失败");
            }
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "下单成功");
        }
        return UtilReturn.resErrorMap("下单失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 材料订单-获取材料清单
     *
     * @return
     */
    @RequestMapping(value = "/supplier/material/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialList(@RequestParam("orderId") String orderId,
                               @RequestParam("supplierNo") String supplierNo) {
        List<MaterialListResp> list = this.administratorService.getMaterialList(orderId, supplierNo);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 材料订单-获取材料订单物流信息
     *
     * @param materialStatusId
     * @return
     */
    @RequestMapping(value = "/deliver/info", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialDeliverInfo(@RequestParam("materialStatusId") Integer materialStatusId) {
        ShipmentRemarkResp shipmentRemarkResp = this.administratorService.getMaterialDeliverInfo(materialStatusId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, shipmentRemarkResp);
    }

    /**
     * 材料管理-获取材料管理列表
     *
     * @param materialNo
     * @param materialClass
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/material/manage/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getMaterialManageInfoList(@RequestParam(value = "materialNo", required = false) String materialNo,
                                         @RequestParam(value = "materialClass", required = false) String materialClass,
                                         @RequestParam(value = "supplierId", required = false) String supplierId) {
        List<MaterialManageResp> list = this.administratorService.getMaterialManageInfoList(materialNo, materialClass, supplierId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 材料管理-添加材料
     *
     * @param materialManageResp
     * @return
     */
    @RequestMapping(value = "/material/manage/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addMaterialInfo(@RequestBody MaterialManageResp materialManageResp) {
        int result = this.administratorService.addMaterialInfo(materialManageResp);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "添加成功");
        }
        return UtilReturn.resErrorMap("添加失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 材料管理-获取材料详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/material/manage/detail", method = RequestMethod.POST)
    @ResponseBody
    public Map getMaterialInfoDetail(@RequestParam("id") Integer id) {
        MaterialManageResp materialManageResp = this.administratorService.getMaterialInfoDetail(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, materialManageResp);
    }

    /**
     * 材料管理-修改材料
     *
     * @param materialManageResp
     * @return
     */
    @RequestMapping(value = "/material/manage/modify", method = RequestMethod.POST)
    @ResponseBody
    public Map modifyMaterialInfo(@RequestBody MaterialManageResp materialManageResp) {
        int result = this.administratorService.modifyMaterialInfo(materialManageResp);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "添加成功");
        }
        return UtilReturn.resErrorMap("添加失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 材料管理-删除材料
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/material/manage/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteMaterialInfo(@RequestParam("id") Integer id) {
        int result = this.administratorService.deleteMaterialInfo(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 奖励记录-获取奖励记录列表
     *
     * @param userName
     * @param rewardStatus
     * @return
     */
    @RequestMapping(value = "/reward/history/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getRewardHistoryList(@RequestParam(value = "userName", required = false) String userName,
                                    @RequestParam(value = "rewardStatus", required = false) String rewardStatus) {
        List<RewardHistoryResp> list = this.administratorService.getRewardHistoryList(userName, rewardStatus);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 奖励记录-删除奖励记录
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/reward/history/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteRewardHistory(@RequestParam("id") Integer id) {
        int result = this.administratorService.deleteRewardHistory(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 检查用户和项目经理端是否提交完成
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/check/confirm", method = RequestMethod.GET)
    @ResponseBody
    public Map checkConfirm(@RequestParam("orderId") String orderId) {
        int result = this.administratorService.checkConfirm(orderId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "可以确认");
        }
        return UtilReturn.resErrorMap("不可确认", UtilReturn.ERROR_CODE);
    }

    /**
     * 账单管理-获取账目列表
     */
    @RequestMapping(value = "/bill/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getBillList(@RequestParam(value = "orderId", required = false) String orderId,
                           @RequestParam(value = "identification", required = false) String identification,
                           @RequestParam(value = "type", required = false) String type,
                           @RequestParam(value = "payType", required = false) String payType) {
        List<BillResp> list = this.administratorService.getBillList(orderId, identification, type,payType);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 材料管理-获取材料管理列表
     */
    @RequestMapping(value = "/bill/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteBillInfo(@RequestParam("id") Integer id) {
        int result = this.administratorService.deleteBillInfo(id);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败", UtilReturn.ERROR_CODE);
    }
}
